﻿using System;
using System.Collections;
using System.Web.Mvc;
using WeBlog.Interfaces;
using WeBlog.Models;
using WeBlog.Repositories;
using WeBlog.Resources;

namespace WeBlog.Controllers {
	public class TagController : ApplicationController
	{
		public TagController(IUnitOfWork unitOfWork)
			: base( unitOfWork )
		{            
		}

        [WeBlogAuthorize(Role.Admin, Role.Author, Role.Editor), Theme("Admin")]
		public ActionResult Manage(GridSettings settings)
		{
			var model = new ManageTagsViewModel(settings, UnitOfWork);
			UpdateModel( model );
			return View( model );
		}

		[HttpPost, WeBlogAuthorize(Role.Admin, Role.Author, Role.Editor), Theme("Admin")]
		public ActionResult Delete(int[] tagIds)
		{
			var repository = new TagRepository(UnitOfWork);
			foreach (var id in tagIds)
			{
				var tag = repository.Find(id);
				if (tag != null)
				{
					foreach( var post in tag.Posts )
					{
						post.Tags.Remove(tag);
					}
					repository.Remove(tag);
				}
			}
			UnitOfWork.Commit();
			return RedirectToAction("Manage");
		}

		public ActionResult Details(GridSettings settings, string name)
		{
			var model = new TagViewModel(UnitOfWork, settings, name);
			UpdateModel(model);
			return View(model);
		}        

		[HttpPost, WeBlogAuthorize(Role.Admin, Role.Author, Role.Editor ), Theme("Admin")]
		public JsonResult Create(string name)
		{
			if( String.IsNullOrWhiteSpace( name ) )
				return Json( new JsonResponse{ Success = false, Message = "Name is required" });

			try
			{
				var repository = new TagRepository(UnitOfWork);
				bool exists = repository.FindByName(name) != null;
				if( exists )
					return Json(new JsonResponse { Success = false, Message = String.Format( "A tag with the name \"{0}\" already exists.", name ) });

				Tag tag = new Tag { Name = name };
				repository.Add(tag);
				UnitOfWork.Commit();                
				return Json(new JsonResponse { Success = true });
			}
			catch (Exception ex)
			{
				return Json(new JsonResponse { Success = false, Message = ex.Message });
			}                        
		}

		[HttpPost, WeBlogAuthorize(Role.Admin, Role.Author, Role.Editor), Theme("Admin")]
		public ActionResult Edit(int id, string name)
		{
			if (String.IsNullOrWhiteSpace(name))
				return Json(new JsonResponse { Success = false, Message = Strings.Required });

			try
			{
				var repository = new TagRepository(UnitOfWork);
				Tag tag = repository.Find(id);
				tag.Name = name;
				UnitOfWork.Commit();                
				return Json(new JsonResponse { Success = true });
			}
			catch (Exception ex)
			{
				return Json(new JsonResponse { Success = false, Message = ex.Message });
			}  
		}        

		public JsonResult TagSuggestion(string tag)
		{
			var tags = new ArrayList();
			var repository = new TagRepository(UnitOfWork);
			foreach (var t in repository.FindAll())
			if (t.Name.StartsWith(tag , StringComparison.InvariantCultureIgnoreCase))
				tags.Add(t.Name);

			return Json(tags, JsonRequestBehavior.AllowGet);
		}
	}
}
